import org.apache.commons.io.FileUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.List;

/**
 * Created by mengwei666 on 2017/1/7.
 */
public class Download {
    public static int i=0;
    public static void main(String[] args)throws IOException {
        DBOpener dbOpener=new DBOpener();
        List sort=dbOpener.sort();   //将每个分区的前三名的ibilibili链接保存到List
        for(int i=30;i<35;i++) {
            String _url=sort.get(i).toString();
            Document doc = Jsoup.connect(_url).get();
            Elements links = doc.select("a[href]:contains(视频下载)");
            String download = links.attr("href");
            System.out.println(download);
            String res = downloadFromUrl(download, "C:\\Users\\mengwei666\\Desktop\\homework_5\\");
            System.out.println(res);
        }
    }
    public static String downloadFromUrl(String url,String dir) {
        try {
            URL httpurl = new URL(url);
            File f = new File(dir+"视频"+(i++) +".mp4");
            FileUtils.copyURLToFile(httpurl, f);
        }
        catch(java.net.MalformedURLException e){

        }
        catch (Exception e) {
            e.printStackTrace();
            return "Fault!";
        }
        return "Successful!";
    }
}
